/*

  Implementation of terrain functions.

  @author Richard Kelley, Liesl Wigand

 */

#include <fstream>
#include <iostream>

#include "terrain.h"
#define SCALE 30.0

namespace blackjack {

  // Initialize member variables to sensible defaults
  Terrain::Terrain() {
    minElevation = maxElevation = 0.0;
  }

  void Terrain::setDimensions(int w, int h) {
    width = w;
    height = h;
  }

  void Terrain::setMaxHeight(int h) {
    maxHeight = h;
  }

  void Terrain::setTerrainData(int *d) {
    data = d;
  }


  void Terrain::drawTerrain(){
    int *terrainData = getTerrainData();

    int height = getHeight();
    int width = getWidth();

    for (int i = 0; i < height-1; ++i) {
      
        for (int j = 0; j < width-1; ++j) {
          if (terrainData[i * height + j] *
              terrainData[(i+1)*height + j] *
              terrainData[(i+1)*height + j+1] *
              terrainData[i * height + j+1] > 0)
            glColor3f(0.0, 1.0, 0.0);
          else
            glColor3f(0.0, 0.0, 1.0);

          glBegin(GL_QUADS);
          glVertex3f(i, (float)terrainData[i * height + j] / SCALE, j);
          glVertex3f(i+1, (float)terrainData[(i+1)*height + j] / SCALE,  j);
          glVertex3f(i+1, (float)terrainData[(i+1)*height + j+1] / SCALE, j+1);
          glVertex3f(i, (float)terrainData[i * height + j+1] / SCALE, j+1);
          glEnd();

          if (terrainData[i * height + j] *
              terrainData[(i+1)*height + j] *
              terrainData[(i+1)*height + j+1] *
              terrainData[i * height + j+1] > 0) {

            glColor3f(.5, 0.0, 0.2);

            glBegin(GL_LINE_LOOP);
            glVertex3f(i, (float)terrainData[i * height + j] / SCALE, j);
            glVertex3f(i+1, (float)terrainData[(i+1)*height + j] / SCALE,  j);
            glVertex3f(i+1, (float)terrainData[(i+1)*height + j+1] / SCALE, j+1);
            glVertex3f(i, (float)terrainData[i * height + j+1] / SCALE, j+1);
            glEnd();
          }

        }
    }
    //qDebug()<<"Drew terrain";
  }
}
